<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\util\ReturnCode;

class CheckItem extends Base
{
    public function lists()
    {
        $where = [];
        $model = new \app\model\CheckType();
        // 传特定值查全部
        $all = $this->request->get('all', 0, 'intval');
        if ($all) {
            $list = $model->where('status', 1)->order('sort', 'desc')->select()->toArray();
            $data = [
                'list' => $list,
                'count' => count($list)
            ];
            return $this->buildSuccess($data);
        }
        $keywords = $this->request->get('keywords', '');

        if ($keywords) {
            $where[] = ['name', 'like', '%' . $keywords . '%'];
        }
        $page = $this->request->get('page', 1, 'intval');
        $size = $this->request->get('size', 10, 'intval');
        $list = $model->where($where)->order('sort', 'desc')->paginate($size);
        $data = [
            'list' => $list->items(),
            'count' => $list->total()
        ];
        return $this->buildSuccess($data);
    }

    public function add()
    {
        $model = new \app\model\CheckType();
        $name = $this->request->post('name', '');
        $status = $this->request->post('status', 0);
        $type = $this->request->post('type', 2);
        $direction = $this->request->post('direction', 1);
        $sort = $this->request->post('sort', 0);
        $data = [
            'name' => $name,
            'status' => $status,
            'type' => $type,
            'direction' => $direction,
            'sort' => $sort,
            'create_time' => time()
        ];
        $model->insert($data);
        return $this->buildSuccess();
    }

    public function edit()
    {
        $model = new \app\model\CheckType();
        $id = $this->request->post('id', 0);
        $name = $this->request->post('name', '');
        $status = $this->request->post('status', 0);
        $type = $this->request->post('type', 2);
        $direction = $this->request->post('direction', 1);
        $sort = $this->request->post('sort', 0);
        $data = [
            'name' => $name,
            'status' => $status,
            'type' => $type,
            'direction' => $direction,
            'sort' => $sort,
        ];
        $model->where('id', $id)->update($data);
        return $this->buildSuccess();
    }

    public function del()
    {
        $model = new \app\model\CheckType();
        $id = $this->request->post('id', 0);
        // id check_record有使用就软删除，没有就直接删除
        $checkRecordModel = new \app\model\CheckItem();
        $checkRecord = $checkRecordModel->where('type_id', $id)->find();
        if ($checkRecord) {
            $model::destroy($id);
        } else {
            $model::destroy($id, true);
        }
        return $this->buildSuccess();
    }

    public function changeStatus()
    {
        $id = $this->request->post('id');
        $status = $this->request->post('status');
        $res = \app\model\CheckType::update([
            'status' => $status
        ], ['id' => $id]);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }

    // sort
    public function changeSort()
    {
        $id = $this->request->post('id');
        $sort = $this->request->post('sort');
        $res = \app\model\CheckType::update([
            'sort' => $sort
        ], ['id' => $id]);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }
}
